<?php

namespace app\admin\controller;

use app\customer\curd\CreateInterface;
use app\customer\curd\DeleteInterface;
use app\customer\curd\DetailInterface;
use app\customer\curd\PagingInterface;
use app\customer\curd\UpdateInterface;
use app\consts\Code;
use app\exception\BizException;
use support\Log;
use support\Request;

/**
 * 增删改查操作控制器
 */
class CurdController extends AdminController
{
    public function __construct()
    {
        $class = request()->route->param("model");
        //必须存在这个业务模型
        if(!class_exists($class)){
            throw new BizException(Code::BIZ_MODEL_NOT_EXISTS);
        }
    }
    /**
     * 分页查询方法
     * @param Request $request
     * @return \support\Response
     * @throws BizException
     */
    public function paging(Request $request)
    {
        $model = new ($request->route->param("model"));
        //必须实现业务接口
        if(!$model instanceof PagingInterface){
            throw new BizException(Code::BIZ_MODEL_ERROR);
        }
        $validator = validator($request->all(), $model->rules(), $model->messages());
        //参数验证
        if($validator->fails()){
            return  $this->invlid($validator->errors()->toArray());
        }
        return $this->paginator($model->paging($validator->validated()));
    }

    /**
     * 创建记录
     * @param Request $request
     * @return \support\Response
     * @throws BizException
     */
    public function create(Request $request)
    {
        $model = new ($request->route->param("model"));
        //必须实现业务接口
        if(!$model instanceof CreateInterface){
            throw new BizException(Code::BIZ_MODEL_ERROR);
        }
        $validator = validator($request->all(), $model->rules(), $model->messages());
        //参数验证
        if($validator->fails()){
            return  $this->invlid($validator->errors()->toArray());
        }
        $created =  $model->bizCreate($validator->validated());
        return $this->ok([
            "id" => $created->id
        ]);
    }

    /**
     * 更新记录
     * @param Request $request
     * @return \support\Response
     * @throws BizException
     */
    public function update(Request $request,$id)
    {
        $model = new ($request->route->param("model"));
        //必须实现业务接口
        if(!$model instanceof UpdateInterface){
            throw new BizException(Code::BIZ_MODEL_ERROR);
        }
        $validator = validator($request->all(), $model->rules(), $model->messages());
        //参数验证
        if($validator->fails()){
            return  $this->invlid($validator->errors()->toArray());
        }
        $updated =  $model->bizUpdate($id, $validator->validated());
        return $this->ok([
            "updated" => $updated
        ]);
    }

    /**
     * 根据id查询详情
     * @param Request $request
     * @param int $id
     * @return \support\Response
     * @throws BizException
     */
    public function detail(Request $request,int $id)
    {
        $model = new ($request->route->param("model"));
        //必须实现业务接口
        if(!$model instanceof DetailInterface){
            throw new BizException(Code::BIZ_MODEL_ERROR);
        }
        return $this->ok([
            "detail" => $model->detail($id)
        ]);
    }

    /**
     * 根据id删除记录
     * @param Request $request
     * @return \support\Response
     * @throws BizException
     */
    public function delete(Request $request,$id)
    {
        $model = new ($request->route->param("model"));
        //必须实现业务接口
        if(!$model instanceof DeleteInterface){
            throw new BizException(Code::BIZ_MODEL_ERROR);
        }
        return $this->ok([
            "deleted" => $model->bizDelete($id)
        ]);
    }
}